const { application } = require("express");
let mysql = require("mysql")
const express = require('express')
const cors = require('cors')
const app = express();
app.use(cors())
let connection = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "hsp",
    database: "db04"
});

connection.connect((err) => {
    if (err) {
        console.error("连接失败：" + err.stack);
        return;
    }
    console.log("连接成功!");
})


//编写前端用户学习数据接口
app.get('/userstudent', (err, res) => {

    connection.query("SELECT * FROM interest WHERE username = ", (err, result) => {

        if (err) {
            console.error("查询出错：" + err.message);
            return;
        }

        res.json(result)
    });
})



//编写查看关注API接口
app.get('/interests', (err, res) => {

    connection.query("SELECT * FROM interest", (err, result) => {

        if (err) {
            console.error("查询出错：" + err.message);
            return;
        }

        res.json(result)
    });
})

app.get('/study', (err, res) => {
    console.log(err.url)
    const http = require('http');
    const querystring = require('querystring');
    const queryObject = querystring.parse(err.url.split('?')[1]);
    console.log(queryObject)
    const username = queryObject['username'];
     const sql = 'SELECT * FROM users WHERE username=?'
     connection.query(sql, [username],(err, result) => {
         if (err) {
             console.error("查询出错：" + err.message);
             return;
         }
         
         res.json(result)
     });
 })



//编写添加关注接口
app.get('/addinterest', (err, res) => {
    console.log(err.url)
    const http = require('http');
    const querystring = require('querystring');
    const queryObject = querystring.parse(err.url.split('?')[1]); 
    console.log(queryObject);   
    const name = queryObject['form[name]'];
    const introduce = queryObject['form[introduce]'];
    const status = queryObject['form[status]'];    
     const sql = 'INSERT INTO interest (teacher,name,introduce,status) VALUES("/src/components/huati/img/touxiang4.png",\''+name+'\',\''+introduce+'\',\''+status+'\')';
     connection.query(sql, (err, result) => {
        
         if (err) {
             console.error("添加出错：" + err.message);
             return;
         }   
         res.json(result)
     });
 })


 //编写删除关注接口
app.get('/delinterest', (err, res) => {
    console.log(err.url)
    const http = require('http');
    const querystring = require('querystring');
    const queryObject = querystring.parse(err.url.split('?')[1]);
    console.log(queryObject)
    const id = queryObject['id'];
     const sql = 'DELETE FROM interest WHERE id=?'
     connection.query(sql, [id],(err, result) => {
         if (err) {
             console.error("删除出错：" + err.message);
             return;
         }
         
         res.json(result)
     });
 })


   //编写修改精品课程接口
   app.get('/editorinterest', (err, res) => {
    console.log(err.url)
    const http = require('http');
    const querystring = require('querystring');
    const queryObject = querystring.parse(err.url.split('?')[1]);
    const id = parseInt(queryObject['emanager[id]']);
    const name = queryObject['emanager[name]'];
    const introduce = queryObject['emanager[introduce]'];
    const status = queryObject['emanager[status]'];
    console.log(queryObject)
     const sql = 'UPDATE interest SET name = ?,introduce = ?,status = ? WHERE id=?'
     connection.query(sql,[name,introduce,status,id],(err, result) => {
         if (err) {
             console.error("修改出错：" + err.message);
             return;
         }
         res.json(result)
     });
 })



//编写查看精品课程API接口
app.get('/qualitys', (err, res) => {

    connection.query("SELECT * FROM quality", (err, result) => {

        if (err) {
            console.error("查询出错：" + err.message);
            return;
        }

        res.json(result)
    });
})

//编写添加精品课程接口
app.get('/addquality', (err, res) => {
    console.log(err.url)
    const http = require('http');
    const querystring = require('querystring');
    const queryObject = querystring.parse(err.url.split('?')[1]);
    // console.log(queryObject['form[teacher]']);   
    console.log(queryObject);   
    const teacher = queryObject['form[teacher]'];
    const introduce1 = queryObject['form[introduce1]'];
    const introduce2 = queryObject['form[introduce2]'];
    const price = queryObject['form[price]'];
    const coursenum = queryObject['form[coursenum]'];
    const receive = queryObject['form[receive]'];
    
    // INSERT INTO course (teacher,introduce1,introduce2,`join`,receive) VALUES('./img','123456','123123','22','2')
    //  const sql = 'INSERT INTO users (username,password,root) VALUES(\''+username+'\',\''+password+'\',\''+root+'\')';
     const sql = 'INSERT INTO quality (teacher,introduce1,introduce2,price,`coursenum`,receive) VALUES("./img/500577327@2x.png",\''+introduce1+'\',\''+introduce2+'\',\''+price+'\',\''+coursenum+'\',\''+receive+'\')';
     connection.query(sql, (err, result) => {
        
         if (err) {
             console.error("添加出错：" + err.message);
             return;
         }   
         res.json(result)
     });
 })



//编写删除精品课程接口
app.get('/delquality', (err, res) => {
    console.log(err.url)
    const http = require('http');
    const querystring = require('querystring');
    const queryObject = querystring.parse(err.url.split('?')[1]);
    console.log(queryObject)
    const id = queryObject['id'];
     const sql = 'DELETE FROM quality WHERE id=?'
     connection.query(sql, [id],(err, result) => {
         if (err) {
             console.error("删除出错：" + err.message);
             return;
         }
         
         res.json(result)
     });
 })


  //编写修改精品课程接口
  app.get('/editorquality', (err, res) => {
    console.log(err.url)
    const http = require('http');
    const querystring = require('querystring');
    const queryObject = querystring.parse(err.url.split('?')[1]);
    const id = parseInt(queryObject['emanager[id]']);
    const introduce1 = queryObject['emanager[introduce1]'];
    const introduce2 = queryObject['emanager[introduce2]'];
    const price = queryObject['emanager[price]'];
    const coursenum = queryObject['emanager[coursenum]'];
    const receive = queryObject['emanager[receive]'];
    console.log(queryObject)
    // console.log(introduce1,introduce2,join,receive,id,price)
    // UPDATE course SET introduce1 = 'dsaasd',introduce2 = 'sdad',`join`='123' WHERE id=9
    //  const sql = 'UPDATE users SET username = ?,password = ?,root=? WHERE username=?'
     const sql = 'UPDATE quality SET introduce1 = ?,introduce2 = ?,price = ?,`coursenum`=?,receive = ? WHERE id=?'
     connection.query(sql,[introduce1,introduce2,price,coursenum,receive,id],(err, result) => {
         if (err) {
             console.error("修改出错：" + err.message);
             return;
         }
         res.json(result)
     });
 })




//编写查看课程API接口
app.get('/courses', (err, res) => {

    connection.query("SELECT * FROM course", (err, result) => {

        if (err) {
            console.error("查询出错：" + err.message);
            return;
        }

        res.json(result)
    });
})

//编写添加课程接口
app.get('/addcourse', (err, res) => {
    console.log(err.url)
    const http = require('http');
    const querystring = require('querystring');
    const queryObject = querystring.parse(err.url.split('?')[1]);
    // console.log(queryObject['form[teacher]']);   
    console.log(queryObject);   
    const teacher = queryObject['form[teacher]'];
    const introduce1 = queryObject['form[introduce1]'];
    const introduce2 = queryObject['form[introduce2]'];
    const price = queryObject['form[price]'];
    const join = queryObject['form[join]'];
    const receive = queryObject['form[receive]'];
    
    // INSERT INTO course (teacher,introduce1,introduce2,`join`,receive) VALUES('./img','123456','123123','22','2')
    //  const sql = 'INSERT INTO users (username,password,root) VALUES(\''+username+'\',\''+password+'\',\''+root+'\')';
     const sql = 'INSERT INTO course (teacher,introduce1,introduce2,price,`join`,receive) VALUES("./img/500577327@2x.png",\''+introduce1+'\',\''+introduce2+'\',\''+price+'\',\''+join+'\',\''+receive+'\')';
     connection.query(sql, (err, result) => {
        
         if (err) {
             console.error("添加出错：" + err.message);
             return;
         }   
         res.json(result)
     });
 })
//编写删除课程接口
app.get('/delcourse', (err, res) => {
    console.log(err.url)
    const http = require('http');
    const querystring = require('querystring');
    const queryObject = querystring.parse(err.url.split('?')[1]);
    console.log(queryObject)
    const id = queryObject['id'];
     const sql = 'DELETE FROM course WHERE id=?'
     connection.query(sql, [id],(err, result) => {
         if (err) {
             console.error("删除出错：" + err.message);
             return;
         }
         
         res.json(result)
     });
 })

  //编写修改用户接口
app.get('/editorcourse', (err, res) => {
    console.log(err.url)
    const http = require('http');
    const querystring = require('querystring');
    const queryObject = querystring.parse(err.url.split('?')[1]);
    const id = parseInt(queryObject['emanager[id]']);
    const introduce1 = queryObject['emanager[introduce1]'];
    const introduce2 = queryObject['emanager[introduce2]'];
    const price = queryObject['emanager[price]'];
    const join = queryObject['emanager[join]'];
    const receive = queryObject['emanager[receive]'];
    console.log(queryObject)
    console.log(introduce1,introduce2,join,receive,id,price)
    // UPDATE course SET introduce1 = 'dsaasd',introduce2 = 'sdad',`join`='123' WHERE id=9
    //  const sql = 'UPDATE users SET username = ?,password = ?,root=? WHERE username=?'
     const sql = 'UPDATE course SET introduce1 = ?,introduce2 = ?,price = ?,`join`=?,receive = ? WHERE id=?'
     connection.query(sql,[introduce1,introduce2,price,join,receive,id],(err, result) => {
         if (err) {
             console.error("修改出错：" + err.message);
             return;
         }
         res.json(result)
     });
 })

//编写查看用户API接口
app.get('/users', (err, res) => {

    connection.query("select * from users", (err, result) => {

        if (err) {
            console.error("查询出错：" + err.message);
            return;
        }

        res.json(result)
    });
})

//编写添加用户接口
app.get('/adduser', (err, res) => {
   
   console.log(err.url)
   const http = require('http');
   const querystring = require('querystring');
   const queryObject = querystring.parse(err.url.split('?')[1]);
   console.log(queryObject['form[username]']);   
   const username = queryObject['form[username]'];
   const password = queryObject['form[password]'];
   const root = queryObject['form[root]'];
   const day = queryObject['form[day]'];
   const hour = queryObject['form[hour]'];
   const subject = queryObject['form[subject]'];
    const sql = 'INSERT INTO users (username,password,root,day,hour,subject) VALUES(\''+username+'\',\''+password+'\',\''+root+'\',\''+day+'\',\''+hour+'\',\''+subject+'\')';
    connection.query(sql, (err, result) => {
       
        if (err) {
            console.error("添加出错：" + err.message);
            return;
        }
        
        res.json(result)
    });
})

//编写删除用户接口
app.get('/deluser', (err, res) => {
    console.log(err.url)
    const http = require('http');
    const querystring = require('querystring');
    const queryObject = querystring.parse(err.url.split('?')[1]);
    console.log(queryObject['username'])
    const username = queryObject['username'];
     const sql = 'DELETE FROM users WHERE username=?'
     connection.query(sql, [username],(err, result) => {
        
         if (err) {
             console.error("删除出错：" + err.message);
             return;
         }
         
         res.json(result)
     });
 })


 //编写修改用户接口
app.get('/editoruser', (err, res) => {
    console.log(err.url)
    const http = require('http');
    const querystring = require('querystring');
    const queryObject = querystring.parse(err.url.split('?')[1]);
    const username = queryObject['emanager[username]'];
    const password = queryObject['emanager[password]'];
    const root = queryObject['emanager[root]'];
    const day = queryObject['emanager[day]'];
    const hour = queryObject['emanager[hour]'];
    const subject = queryObject['emanager[subject]'];
    console.log(username,password,root)
     const sql = 'UPDATE users SET username = ?,password = ?,root=? ,day=?,hour=?,subject=? WHERE username=?'
     connection.query(sql,[username,password,root,day,hour,subject,username],(err, result) => {
         if (err) {
             console.error("修改出错：" + err.message);
             return;
         }
         res.json(result)
     });
 })


app.listen(8081, () => {
    console.log("Server running at http://127.0.0.1:8081")
})

// connection.query("select * from employee", (err, result) => {

//     if (err) {
//         console.error("查询出错：" + err.message);
//         return;
//     }
//     console.log(result);
// });
// connection.end();



//编写用于前端用户接口
app.get('/addreg', (err, res) => {
   
    console.log(err.url)
    const http = require('http');
    const querystring = require('querystring');
    const queryObject = querystring.parse(err.url.split('?')[1]);
    console.log(queryObject['form[username]']);   
    const username = queryObject['form[username]'];
    const password = queryObject['form[password]'];
    const root = queryObject['form[root]'];
    const day = queryObject['form[day]'];
    const hour = queryObject['form[hour]'];
    const subject = queryObject['form[subject]'];
     const sql = 'INSERT INTO users (username,password,root,day,hour,subject) VALUES(\''+username+'\',\''+password+'\',\''+root+'\',0,0,0)';
     connection.query(sql, (err, result) => {
        
         if (err) {
             console.error("添加出错：" + err.message);
             return;
         }
         
         res.json(result)
     });
 })